package com.web.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.biz.AddressBizImpl;
import com.biz.IAddressBiz;
import com.biz.IOrdersBiz;
import com.biz.IProductBiz;
import com.biz.IShopCartBiz;
import com.biz.OrdersBizImpl;
import com.biz.ProductBizImpl;
import com.biz.ShopCartBizImpl;
import com.entity.Address;
import com.entity.Orders;
import com.entity.OrdersDetails;
import com.entity.Product;
import com.entity.ShopCart;
import com.entity.UserInfo;
import com.util.PageBean;

public class OrdersServlet extends HttpServlet {
	IOrdersBiz osbiz=new OrdersBizImpl();
	IShopCartBiz scbiz=new ShopCartBizImpl();
	IAddressBiz adbiz=new AddressBizImpl();
	IProductBiz pbiz=new ProductBizImpl();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		UserInfo us = (UserInfo) session.getAttribute("loger");
		if(us!=null){
			String act=request.getParameter("act");
			if(act==null){
				System.out.println("******分页显示地址******");
				String uid = "" + us.getId();
				String mypage = request.getParameter("page");
				if (mypage == null)
					mypage = "1";
				int currPage = Integer.parseInt(mypage);
				if (currPage < 1)
					currPage = 1;
				PageBean<Orders> ospb = osbiz.showOrdersByPage(currPage, 3, uid);
				session.setAttribute("ospb", ospb);
				
				session.removeAttribute("orders");
				response.sendRedirect("showOrders.jsp");
			}else if(act.equals("add")){
				int uid=us.getId();
				
				String addressid=request.getParameter("addressid");
				Address ad=adbiz.getAddress(addressid);
				
				Orders os=new Orders();
				os.setUid(""+uid);
				os.setAdid(ad.getId());
				
				List<OrdersDetails> odslist=new ArrayList<OrdersDetails>();
				
				String pid=(String) session.getAttribute("pid");
				
				if(pid==null || pid.equals("null")){
					String[] ids=(String[]) session.getAttribute("scids");
					ShopCart sc=null;
					OrdersDetails ods=null;
					for (String id : ids) {
						sc=scbiz.getShopCart(id, ""+uid);
						ods=new OrdersDetails();
						ods.setPid(""+sc.getPid());
						ods.setPrice(sc.getPrice());
						ods.setNum(sc.getNum());
						ods.setComments(sc.getComments());
						odslist.add(ods);
					}
					session.removeAttribute("scids");
					session.removeAttribute("orders");
					if(osbiz.addOrder(os, odslist)>0){
						out.print("<script>alert('订单已提交');location='orders';</script>");
					}
				}else {
					Product pro=pbiz.getPro(Integer.parseInt(pid));
					int stock=pro.getStock();
					int num=Integer.parseInt((String) session.getAttribute("num"));
					if(stock<num){
						out.print("<script>alert('库存不足,购买失败');history.back();</script>");
					}else{
						double price=pro.getPrice();
						String comments=(String) session.getAttribute("comments");
						OrdersDetails ods=new OrdersDetails();
						ods.setPid(pid);
						ods.setNum(num);
						ods.setPrice(price);
						ods.setComments(comments);
						odslist.add(ods);
						session.removeAttribute("pid");
						session.removeAttribute("num");
						session.removeAttribute("comments");
						session.removeAttribute("orders");
						if(osbiz.addOrder(os, odslist)>0){
							out.print("<script>alert('订单已提交');location='orders';</script>");
						}
					}
				}
			}else if(act.equals("del")){
				String id=request.getParameter("id");
				if(osbiz.delOrder(id)<=0){
					out.print("<script>alert('删除失败');</script>");
				}
				out.print("<script>location='orders';</script>");
			}else if(act.equals("mod2")){
				String id=request.getParameter("id");
				if(osbiz.modOrderState_2(id)<=0){
					out.print("<script>alert('操作失败');</script>");
				}
				out.print("<script>location='orders';</script>");
			}
		}else {
			out.print("<script>location='../login.jsp';</script>");
		}
		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
